/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.controller;

import com.tamnhat.entity.Publisher;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Administrator
 */
public class PublisherController implements Serializable {

    private String id;
    private String code;
    private String name;
    private String nameS;
    private DataModel items;
    private Publisher currentPublisher = new Publisher();
    private Publisher oldCurrentPublisher= new Publisher();
    private List<Publisher> list = new ArrayList<Publisher>();

    /**
     * Creates a new instance of PublisherController
     */
    public PublisherController() {
        try {
            list = currentPublisher.allPub("");
            items = new ListDataModel(list);
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
    }

    public Publisher getOldCurrentPublisher() {
        return oldCurrentPublisher;
    }

    public void setOldCurrentPublisher(Publisher oldCurrentPublisher) {
        this.oldCurrentPublisher = oldCurrentPublisher;
    }

    public List<Publisher> getList() {
        return list;
    }

    public void setList(List<Publisher> list) {
        this.list = list;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNameS() {
        return nameS;
    }

    public void setNameS(String nameS) {
        this.nameS = nameS;
    }

    public DataModel getItems() {
        return items;
    }

    public void setItems(DataModel items) {
        this.items = items;
    }

    public Publisher getCurrentPublisher() {
        return currentPublisher;
    }

    public void setCurrentPublisher(Publisher currentPublisher) {
        this.currentPublisher = currentPublisher;
    }

    public String search() {
        try {
            list = currentPublisher.allPub(" where upper(name) like '%" + nameS.toUpperCase() + "%' ");
            items = new ListDataModel(list);
            int i = items.getRowCount();
            if (i > 0) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có " + i + " bản ghi được tìm thấy!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public boolean checkInput() {
        if (currentPublisher.getCode() == null || currentPublisher.getName() == null || currentPublisher.getCode().equalsIgnoreCase("") || currentPublisher.getName().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không được để trống các trường thông tin!"));
            return false;
        }
        return true;
    }
    
    public String updateInsert(){
        currentPublisher = new Publisher();
        return "";
    }

    public String insert() {
        try {
            if (checkInput()) {
                if (currentPublisher.insert()) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật thành công!"));
                    list.add(currentPublisher);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public String updateForEdit() {
        currentPublisher = (Publisher) items.getRowData();
        oldCurrentPublisher = currentPublisher;
        return "";
    }

    public String update() {
        try {
            if (checkInput()) {
                if (currentPublisher.update()) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật thành công!"));                   
                } else {
                    oldCurrentPublisher = currentPublisher;
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            oldCurrentPublisher = currentPublisher;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public String remove() {
        try {
            currentPublisher = (Publisher) items.getRowData();
            if (currentPublisher.remove()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));
                list.remove(currentPublisher);
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }
}